#include <string>
#include <algorithm>

using namespace std;

class Solution {
public:
    int nextGreaterElement(int n) {
        string num(to_string(n));
        long long temp = -1;
        if (next_permutation(num.begin(), num.end())) {
            temp = stoll(num);
        }
        if (temp > INT32_MAX) {
            temp = -1;
        }
        return temp;
    }
};